#include "moreSingalWidget.h"
#include<QPushButton>
#include<QVBoxLayout>
#include<QLabel>
#include"debug.h"

/////////////////////
/// 组合控件1,头像加名字
///////////////

avatarItem::avatarItem(const QIcon &avatarpix, const QString &name)
{
    this->setFixedSize(70,80);


    QVBoxLayout*layout=new QVBoxLayout();
    this->setLayout(layout);
    layout->setSpacing(10);
    layout->setContentsMargins(0,0,0,0);
    layout->setAlignment(Qt::AlignHCenter);

    //创建头像
    avatar=new QPushButton();
    avatar->setIcon(avatarpix);
     avatar->setFixedSize(45,45);
     avatar->setIconSize(QSize(45,45));
     avatar->setStyleSheet("QPushButton{border:none;background-color:transparent;}");

       layout->addWidget(avatar,Qt::AlignHCenter);

      //创建名字
      nameLabel=new QLabel();
      QFont font("微软雅黑",12);
      nameLabel->setFont(font);
      nameLabel->setStyleSheet("QLabel{border:none;background-color:transparent;}");
      nameLabel->setAlignment(Qt::AlignHCenter);
       layout->addWidget(nameLabel,Qt::AlignHCenter);


       //对名字进行截断处理
       const int MAX_WIDTH = 65;
       QFontMetrics metrics(font);
       int totalWidth = metrics.horizontalAdvance(name);
       if (totalWidth >= MAX_WIDTH) {
           // 需要截断
           QString tail = "...";
           int tailWidth = metrics.horizontalAdvance(tail);
           int availableWidth = MAX_WIDTH - tailWidth;
           int availableSize = name.size() * ((double)availableWidth / totalWidth);
           QString newName = name.left(availableSize);
           nameLabel->setText(newName + tail);
       }

       // 6.
       layout->addWidget(avatar);
       layout->addWidget(nameLabel);

}






///////////////////////////////
//创造一个类，打开...跳出来的更多
////////////////////////////
moreSignalWidget::moreSignalWidget(QWidget*parent)
    :QDialog(parent)
{
    //设置基本属性
    this->setWindowTitle("会话详情");
    this->setWindowIcon(QIcon(":/resource/Image/wechat.png"));
    this->setFixedSize(500,500);
    this->move(QCursor::pos());
    this->setAttribute(Qt::WA_DeleteOnClose);
    //增加布局管理器
    QGridLayout*layout=new QGridLayout();
    this->setLayout(layout);
    layout->setSpacing(10);
    layout->setContentsMargins(50,0,50,0);
     //创建一个增加
    avatarItem* additem=new  avatarItem(QIcon(":/resource/Image/add.png"),"添加");
    layout->addWidget(additem,0,0);
    connect( additem->getavatar(),&QPushButton::clicked,this,[=](){
        choosefriendsdialog* choose=new choosefriendsdialog(this,userInfo.userId);
        choose->exec();
    });

    //临时数据
#if TEST_1

    avatarItem* useritem=new  avatarItem(QIcon(":/resource/Image/useravatar1.png"),"张三");
    layout->addWidget(useritem,0,1);

#endif

    //还有一个删除好友的按钮
    deleteFriendBtn=new QPushButton();
    deleteFriendBtn->setFixedHeight(50);
    QString styleBtn="QPushButton{border:none;rgb(90,90,90);border-radius:5px}";
    styleBtn+="QPushButton:pressed{background-color:rgb(235,235,235)}";
     deleteFriendBtn->setStyleSheet(styleBtn);
    layout->addWidget( deleteFriendBtn,1,0,1,3);
    // 6. 添加信号槽, 处理点击 "创建群聊" 按钮
    connect(additem->getavatar(), &QPushButton::clicked, this, [=]() {
        choosefriendsdialog* chooseFriendDialog = new choosefriendsdialog(this, userInfo.userId);
        chooseFriendDialog->exec();
    });

}



